package com.boot.ys.mybatisplus.Test;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;

import java.sql.*;

import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.schema.Table;

/**
 * 描述 读取数据库表字段信息
 *
 * @Author yangsheng
 * @Date 2025/6/6 17:03
 */
public class SelectStarFieldExtractor {

    public static void main(String[] args) {
        String sql = "SELECT * FROM department";
        extractFieldsFromSelectStar(sql, "jdbc:mysql://localhost:3306/springboot3txar", "root", "123456");
    }

    public static void extractFieldsFromSelectStar(String sql, String dbUrl, String user, String password) {
        try {
            // 1. 解析SQL获取表名
            Statement statement = CCJSqlParserUtil.parse(sql);
            if (statement instanceof Select) {
                Select select = (Select) statement;
                PlainSelect plainSelect = (PlainSelect) select.getSelectBody();

                if (plainSelect.getSelectItems().stream().anyMatch(
                        item -> item.toString().equals("*"))) {

                    Table table = (Table) plainSelect.getFromItem();
                    String tableName = table.getName();

                    // 2. 连接数据库获取表结构
                    try (Connection conn = DriverManager.getConnection(dbUrl, user, password)) {
                        DatabaseMetaData metaData = conn.getMetaData();

                        System.out.println("表 " + tableName + " 的字段信息:");
                        System.out.println("+----------------------+----------------------+----------------------+");
                        System.out.println("| 字段名               | 字段类型             | 字段注释             |");
                        System.out.println("+----------------------+----------------------+----------------------+");

                        try (ResultSet columns = metaData.getColumns("springboot3txar", null, tableName + "%", null)) {
                            while (columns.next()) {

                                String columnName = columns.getString("COLUMN_NAME");// 字段名
                                String columnType = columns.getString("TYPE_NAME");// 字段类型
                                String remarks = columns.getString("REMARKS"); // 获取字段注释

                                // 处理可能为null的注释
                                remarks = remarks != null ? remarks : "无注释";

                                // 格式化输出，使用printf保持对齐
                                System.out.printf("| %-20s | %-20s | %-20s |%n",
                                        columnName, columnType, remarks);
                            }
                        }
                        System.out.println("+----------------------+----------------------+----------------------+");
                    }
                } else {
                    System.out.println("这不是SELECT *语句");
                }
            }
        } catch (JSQLParserException | SQLException e) {
            System.err.println("处理错误: " + e.getMessage());
        }
    }

}

